package client.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class ConnectionDialog extends JDialog
{
	private JLabel _L_address;
	private JLabel _L_port;
	
	private JTextField _TF_address;
	private JTextField _TF_port;
	
	private GridBagLayout _formGrid;
	
	private JButton _B_ok;
	private JButton _B_cancel;
	
	private String _address;
	private String _port;
	
	public ConnectionDialog(JFrame parent)
	{
	    super(parent, "Connexion", true);
	    build();
	}
	
	private int _result;
	
	public void build()
	{
		_formGrid = new GridBagLayout();
		JPanel P_fields = new JPanel();
		P_fields.setLayout(_formGrid);
	    
		GridBagConstraints constraints;

		// Champs adresse
		constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.NONE;
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.insets = new Insets(0,5,0,0);
	    _L_address = new JLabel("Adresse ");
	    P_fields.add(_L_address, constraints);
	    
	    constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridx = 1;
		constraints.gridy = 0;
		constraints.weightx = 1.0;
		constraints.insets = new Insets(0,0,0,5);
	    _TF_address = new JTextField();
	    P_fields.add(_TF_address, constraints);
	    
	    // Champs port
	    constraints = new GridBagConstraints();
	    constraints.fill = GridBagConstraints.NONE;
		constraints.gridx = 0;
		constraints.gridy = 1;
		constraints.insets = new Insets(5,5,0,0);
	    _L_port = new JLabel("Port ");
	    P_fields.add(_L_port, constraints);
	    
	    constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.HORIZONTAL;
		constraints.gridx = 1;
		constraints.gridy = 1;
		constraints.weightx = 1.0;
		constraints.insets = new Insets(5,0,0,5);
	    _TF_port = new JTextField();
	    P_fields.add(_TF_port, constraints);
	    
	    // Boite de commande
	    _B_ok = new JButton("OK");
	    _B_cancel = new JButton("Annuler");
	    
	    Box hBox_commands = Box.createHorizontalBox();
	    hBox_commands.add(_B_ok);
	    hBox_commands.add(_B_cancel);
	    
	    // Boite principale
	    Box vBox_main = Box.createVerticalBox();
	    vBox_main.add(P_fields);
	    vBox_main.add(hBox_commands);
	    
	    Container contentPane = getContentPane();
	    contentPane.add(vBox_main,BorderLayout.CENTER);	// Place la boite principale dans la fenêtre
	    
	    setLocationRelativeTo(null);
	    setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);

	    setMinimumSize(new Dimension(250, 160));
	    
	    _B_ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				_address = _TF_address.getText();
				_port = _TF_port.getText();
				_result = JOptionPane.OK_OPTION;
				superSetVisible(false);
			}
	    });
	    
	    _B_cancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				_result = JOptionPane.CANCEL_OPTION;
				superSetVisible(false);
			}
		});
	}
	
	public int showDialog()
	{
		super.setVisible(true);
		return _result;
	}
	
	public void setVisible(boolean b)
	{
		if (b && !this.isVisible())
		{
			_result = JOptionPane.CLOSED_OPTION;
			superSetVisible(b);
		}
		else if (!b && this.isVisible())
		{
			_result = JOptionPane.CLOSED_OPTION;
			superSetVisible(b);
		}
	}
	
	protected void superSetVisible(boolean b)
	{
		super.setVisible(b);
	}
	
	public String getAddress()
	{
		return _address;
	}
	
	public String getPort()
	{
		return _port;
	}
	
	public int getResult()
	{
		return _result;
	}
}
